<template>
    <el-scrollbar height="610px">
  <!-- form 查询表单 -->
    <el-form :model="adress" ref="formRef" label-width="auto" label-position="right" style="padding-bottom: 10px;">
        <el-row :gutter="20">
            <el-col :span="8">
                <el-form-item label="收货人" prop="name">
                    <el-input v-model="adress.name" placeholder="请输入收货人" clearable/>
                </el-form-item>
            </el-col>
            <el-col :span="8">
                <el-form-item label="电话" prop="phone">
                    <el-input v-model="adress.phone" placeholder="请输入电话" clearable/>
                </el-form-item>
            </el-col>
            <el-col :span="8">
                <el-form-item label="地址" prop="address">
                    <el-input v-model="adress.address" placeholder="请输入收货地址" clearable/>
                </el-form-item>
            </el-col>
        </el-row>
        <el-row :gutter="20">
            <el-col :span="24">
                <el-button type="primary" @click="doList">查询</el-button>
                <el-button type="default" @click="resetForm(formRef)">重置</el-button>
                <el-button type="success" @click="showAddDialog">新增</el-button>
            </el-col>
        </el-row>
    </el-form>

  <!-- table 表格 -->
    <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column type="index" lable="序号" width="80"/>
        <el-table-column prop="name" label="收货人" />
        <el-table-column prop="phone" label="手机号" />
        <el-table-column prop="address" label="收货地址" />
        <el-table-column label="默认收货地址" >
          <template #default="scope">
            <el-tag v-if="scope.row.is_default === 1" type="success">是</el-tag>
            <el-tag v-else type="danger">否</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="region" label="分区" sortable/>
        <el-table-column label="操作" width="300">
            <template #default="scope">
                <el-button type="warning" @click="showUpdateDialog(scope.row)">更改</el-button>
                <el-button type="danger" @click="doDelete(scope.row.id)">删除</el-button>
            </template>
        </el-table-column>
    </el-table>

  <!-- 新增 dialog-->
    <el-dialog v-model="addDialogVisible" title="新增收货地址" width="40%" top="2vh" destroy-on-close>
        <add-page
                @flushPage="doFlushPage"
                @closeDialog="addDialogVisible = false">
        </add-page>
    </el-dialog>

  <!-- 更新 dialog-->
    <el-dialog v-model="updateDialogVisible" title="更改收货地址" width="40%" top="2vh" destroy-on-close>
        <update-page ref="updatePageRef"
                     @flushPage="doFlushPage"
                     @closeDialog="updateDialogVisible = false">
        </update-page>
    </el-dialog>
    </el-scrollbar>
</template>

<script setup lang="ts">
import {ref, onMounted, nextTick} from 'vue'
import {ElMessage, FormInstance} from "element-plus";
import axios from 'axios'
import AddPage from "./AddPage.vue";
import UpdatePage from "./UpdatePage.vue";
import router from "../../../router.ts";

//-------------------重置----------------------
const formRef = ref<FormInstance>();
const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
}

//--------------------------获取用户信息-----------------------
const user=ref({
    id:'',
    yhm:'',
    pwd:'',
    yhtx:'',
    sjh:'',
    age:'',
    sex:''
});
const acquireUsers=async ()=>{
    const {data} = await axios.get('/api/user1',{params:user.value});
    if(data.length==0)
        router.push('/login');
    user.value=data;
    adress.value.user_id=user.value.id;
    doList();   // 查询数据
}

// form 查询表单绑定的数据对象：adress
const adress = ref({
    user_id:'',
    name:'',// 姓名
    phone:'',// 手机号
    address:'',// 详细地址
    region:'',// 省、市、区
    is_default:0,// 是否默认
});

// table 数据表格绑定的数据对象：tableData
const tableData = ref([]);
// 查询
const doList = async () => {
    const {data} = await axios.get('/api/user_adress/user/'+user.value.id);
    console.log(data);
    tableData.value = data;
}

// 删除
const doDelete = async (id)=>{
    const {data} = await axios.delete('/api/adress/'+id);
    if(data > 0){
        doFlushPage();		// 删除成功，刷新表格数据
        ElMessage.success('删除成功');
    }else{
        ElMessage.error('删除失败');
    }
}
// 刷新页面
const doFlushPage = ()=>{
    resetForm(formRef.value);
     doList();
}

// 初始化
onMounted(()=>{
    acquireUsers();

})
//--------------------------- 新增 对话框 ---------------------------
const addDialogVisible = ref(false);
const showAddDialog = ()=>{
    addDialogVisible.value = true;
}

//--------------------------- 更新 对话框 ---------------------------
const updateDialogVisible = ref(false);
const updatePageRef = ref(); // 组件引用
const showUpdateDialog = (obj)=>{
    updateDialogVisible.value = true;
    nextTick(()=>{
        updatePageRef.value.handleUpdateData(obj);
    })
}


</script>